#include <iostream>

#include <string>
#include <mysql.h>
using namespace std;

const string host = "127.0.0.1";
const string user = "GR7";
const string passwd = "12345678";
const string db = "connect_demon";
const int port = 3306;

int main()
{
    //1、创建MySQL对象
    MYSQL* ms = mysql_init(nullptr);
    //2、连接数据库
    cout << "开始链接数据库" << endl;
    if (mysql_real_connect(ms, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0) == nullptr)
    {
        cerr << "数据库连接失败" << endl;
        return 1;
    }
    cout << "数据库连接成功" << endl;
    mysql_set_character_set(ms, "utf8"); //设置编码格式为utf8

    //3、向数据库表中插入记录
    std:string sql = "insert into user values (4,'赵六',25)";
    if (mysql_query(ms, sql.c_str()) != 0)
    {
        cout << "插入数据失败" << endl;
        return 2;
    }
    cout << "插入数据成功" << endl;

    //4、关闭数据库
    mysql_close(ms);
    cout << "数据库关闭成功" << endl;
    return 0;
}
